package cn.edu.ujn.finallab.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.ujn.finallab.dao.Orders;
import cn.edu.ujn.finallab.dao.Room;
import cn.edu.ujn.finallab.dao.RoomMapper;
import cn.edu.ujn.finallab.util.Page;

@Service
@Transactional
public class RoomServiceImpl implements IRoomService {
	@Autowired
	private RoomMapper roomMapper;

	@Override
	public List<Room> findAllRoom() {
		// TODO Auto-generated method stub
		return roomMapper.selectAllRoom();
	}
	@Override
	public int insertRoom(Room room) {
		// TODO Auto-generated method stub
		int insert = this.roomMapper.insert(room);
		return insert;
	}

	@Override
	public int deleteRoom(int id) {
		// TODO Auto-generated method stub
		int i = this.roomMapper.deleteByPrimaryKey(id);
		return i;
	}

	@Override
	public Room selectByNumber(String number) {
		// TODO Auto-generated method stub
		Room room = this.roomMapper.selectByNumber(number);
		return room;
	}

	@Override
	public int updateRoom(Room room) {
		// TODO Auto-generated method stub
		int i = this.roomMapper.updateByPrimaryKey(room);
		return i;
	}	

	
	public Page<Room> selectByPage(int currentPage) {
		// TODO Auto-generated method stub
		HashMap<String, Object> map = new HashMap<String, Object>();
		Page<Room> page = new Page<Room>();
		//封装当前页
		page.setPage(currentPage);
		//每页显示的数据
		int pageSize = 10;
		page.setSize(pageSize);
		//封装总记录数
		int totalCount = roomMapper.selectRoomCount();
		//封装记录总数
		page.setTotal(totalCount);
		//封装总页数
		double totalcount = totalCount;
		double num = Math.ceil(totalcount / pageSize);
		page.setTotalpage((int)num);
		// 起始的记录值
		map.put("start", (currentPage - 1) * pageSize);
		// 获取每个页面的pageSize
		map.put("size", page.getSize());
		List<Room> list = roomMapper.selectByPageLimit(map);
		page.setRows(list);
		//返回一个page
		return page;
	}
	@Override
	public List<Room> findUnreservedRoomByTypeAndPriceRoom(HashMap<String, Object> map) {
		// TODO Auto-generated method stub		
		return roomMapper.selectUnreservedRoomByTypeAndPrice(map);
	}
	@Override
	public List<Room> findReservedRoomByTypeAndPrice(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		return roomMapper.selectReservedRoomByTypeAndPrice(map);
	}
	
	
	//根据roomType分页查询
	@Override
	public Page<Room> selectByPageOfRoomTYpe(int currentPage, String roomtype) {
		// TODO Auto-generated method stub
		HashMap<String, Object> map = new HashMap<String, Object>();
		Page<Room> page = new Page<Room>();
		page.setPage(currentPage);
		int pageSize = 10;
		page.setSize(pageSize);
		//封装总记录数
		int totalCount = roomMapper.selectRoomCountByRoomType(roomtype);
		//封装记录总数
		page.setTotal(totalCount);
		//封装总页数
		double totalcount = totalCount;
		double num = Math.ceil(totalcount / pageSize);
		page.setTotalpage((int)num);
		// 起始的记录值
		map.put("start", (currentPage - 1) * pageSize);
		// 获取每个页面的pageSize
		map.put("size", page.getSize());
		// 封装客房类型
		map.put("roomtype", roomtype);
		List<Room> list = roomMapper.selectByPageLimitByRoomType(map);
		page.setRows(list);
		//返回一个page
		return page;
	}

}
